#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''Быстрая отправка информационных email-сообщений'''

#--------------------------------------------------------------------------------------------------------------------
from django.conf import settings
from django.core.mail import EmailMessage
from django.template import Template, Context

#------------------------------------------------- настройки отправки почты -----------------------------------------
settings.configure()
settings.EMAIL_HOST = 'mailnew.transasia.ru'
settings.EMAIL_PORT = 25
settings.EMAIL_USE_TLS = False
settings.EMAIL_HOST_USER = None
settings.EMAIL_HOST_PASSWORD = None
settings.SERVER_EMAIL = settings.EMAIL_HOST_USER
settings.DEFAULT_FROM_EMAIL = settings.EMAIL_HOST_USER 
settings.THOUSAND_SEPARATOR = ','
settings.USE_THOUSAND_SEPARATOR = True
settings.NUMBER_GROUPING = 3
settings.INSTALLED_APPS = ('django.contrib.humanize',)

#-------------------------------------------------------- логика ----------------------------------------------------

def configure(server='127.0.0.1',  port=25, usr=None, pwd=None, usetls=False):
    '''Настройка параметров сервера'''
    settings.EMAIL_HOST = server
    settings.EMAIL_PORT = port
    settings.EMAIL_USE_TLS = usetls
    settings.EMAIL_HOST_USER = usr
    settings.EMAIL_HOST_PASSWORD = pwd
    settings.SERVER_EMAIL = settings.EMAIL_HOST_USER
    settings.DEFAULT_FROM_EMAIL = settings.EMAIL_HOST_USER 

def send(data_dict={}, recipients=[], subject='[MESSAGE FROM ROBOT]', template_file=''):
	if template_file:
		t = Template('\r\n'.join(open(template_file,'r').readlines()))
	else:
		t = Template('[MESSAGE FROM ROBOT]')
	c = Context(data_dict)
	msg = EmailMessage(subject, t.render(c), 'sangi_robot@transasia.ru', recipients)
	msg.content_subtype = "html"  # Main content is now text/html
	msg.send()
	
if __name__ == '__main__':
	send(data_dict={'DATA':'VALUE',}, recipients=['soloviov.ae@transasia.ru',], subject='FAST_EMAIL: TEST MESSAGE')
